package com.tourex.ui;

import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.tourex.App;
import com.tourex.BaseActivity;
import com.tourex.C;
import com.tourex.C.bean.Api01;
import com.tourex.C.bean.ImageInfo;
import com.tourex.C.bean.MyLocationInfo;
import com.tourex.C.bean.Tour;
import com.tourex.C.bean.TourProjectInfo;
import com.tourex.R;
import com.tourex.thirdparty.volley.VolleyUtils;
import com.tourex.utils.AnimUtils;
import com.tourex.utils.BaseUtils;

public class TransitionActivity extends BaseActivity {
	private ImageView imgTranstion1;

	private Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_transtion);
		super.onCreate(savedInstanceState);

		useDefaultPendingTransition = false;

		imgTranstion1 = (ImageView) findViewById(R.id.img_transtion1);

		appParamsLoading();
		transition();
	}

	private void appParamsLoading() {
		requestApi01(App.i().mli);
	}

	public void requestApi01(final MyLocationInfo mli) {
		// http://222.85.150.217:8099/appServer/gzly/server!loadI01.do?wd=26.57&city=488&jd=106.71
		Map<String, String> params = BaseUtils.initMap("wd", mli.lat, "jd", mli.lng, "city", mli.cityCode);
		VolleyUtils.postGson(C.api.api01, Api01.class, params, new Response.Listener<Api01>() {
			@Override
			public void onResponse(Api01 api01) {
				initBasic(api01);
				initTourProjectInfo(api01);
			}
		});
	}

	private void initBasic(Api01 api01) {
		if (api01.imgList != null) {
			for (ImageInfo imgInfo : api01.imgList) {
				String imgUrl = imgInfo.img;
				ImageView imageView = new ImageView(this);
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				imageView.setTag(imgUrl);
				imageLoader.get(imgUrl, ImageLoader.getImageListener(imageView, R.color.image_load, R.color.image_error));
			}
		}
	}

	private void initTourProjectInfo(Api01 api01) {
		if (api01.xl != null && api01.xl.size() > 0) {
			for (Tour tour : api01.xl) {
				if (tour.list != null && tour.list.size() > 0) {
					for (TourProjectInfo tpi : tour.list) {
						ImageView imageView = new ImageView(this);
						App.i().imageLoader.get(tpi.img, ImageLoader.getImageListener(imageView, R.color.image_load, R.color.image_error));
					}
				}
			}
		}
	}

	private void transition() {
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				imgTranstion1.startAnimation(AnimUtils.cearteAlpha(1f, 0f));
				imgTranstion1.setVisibility(View.GONE);
			}
		}, 3000);

		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				MainActivity2.actionStart(TransitionActivity.this);
				finish();
			}
		}, 6000);
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_scale_exit);
	}

}
